package com.gt.quality.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gt.quality.config.aspectj.event.OperLogEvent;
import com.gt.quality.config.SpringBeanUtil;
import com.gt.quality.system.entity.SysOperLog;
import com.gt.quality.system.mapper.SysOperLogMapper;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.system.service.SysOperLogService;
import jakarta.annotation.Resource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 操作日志记录(SysOperLog)表服务实现类
 *
 * @author makejava
 * @since 2026-11-27 16:33:46
 */
@Service("sysOperLogService")
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements SysOperLogService {

    @Resource
    private SysOperLogMapper sysOperLogMapper;


    @EventListener
    public void saveOperatorLog(OperLogEvent operLogEvent) {
        SysOperLog operLog = Convert.convert(SysOperLog.class, operLogEvent);
        // 远程查询操作地点
        operLog.setOperLocation("Operation Log Event");
        operLog.setOperTime(LocalDateTime.now());
        baseMapper.insert(operLog);
    }


    /**
     * Description:  分页查询
     *
     * @param limitRequest 分页查询参数
     * @author: makeJava
     * @date: 2026-02-02 14:57:06
     * @return:com.baomidou.mybatisplus.core.metadata.IPage
     */
    @Override
    public IPage<SysOperLog> queryLimitPage(BaseLimitRequest<SysOperLog> limitRequest) {
        SysOperLog requestBody = limitRequest.getRequestBody();
        IPage<SysOperLog> page = getByPageParam(limitRequest);
        JSONObject thisObj = JSONObject.from(requestBody);
        QueryWrapper<SysOperLog> queryWrapper = queryWrapperBuilder(thisObj, limitRequest);
        return sysOperLogMapper.selectPage(page, queryWrapper);
    }

      /**
     * Description:  批量新增优化版
     *
     * @param entityList entityList
     * @author: makeJava
     * @date: 2026-02-02 19:54:59

     */
    @Override
    public Integer saveBatchByEasyBaseMapper(List<SysOperLog> entityList) {

        return sysOperLogMapper.insertBatchSomeColumn(entityList);
    }


    /**
     * 往上传递 SqlSessionFactory
     * @return SqlSessionFactory
     */
    @Override
    public SqlSessionFactory getSqlSessionFactory() {
        return super.getSqlSessionFactory();
    }

    /**
     * 往上传递 Mapper
     * @return Mapper
     */
    @Override
    public Class<?> getThisMapperClass() {
        return SysOperLogMapper.class;
    }

    /**
     * 往上传递 Transaction
     * @return Transaction
     */
    @Override
    public TransactionTemplate getSpringTx() {
        return SpringBeanUtil.getBean(TransactionTemplate.class);
    }
}


